package com.space.school.controller;


import com.space.school.Enum.OrderStatus;
import com.space.school.domain.Info;
import com.space.school.domain.Master;
import com.space.school.service.InfoService;
import com.space.school.service.impl.DetailServiceImpl;
import com.space.school.service.impl.InfoServiceImpl;
import com.space.school.service.impl.MasterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "master",value = "/master")
public class MasterServlet extends HttpServlet {

    private DetailServiceImpl detailService=new DetailServiceImpl();

    private InfoService infoService=new InfoServiceImpl();

    private MasterServiceImpl masterService=new MasterServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String order_id=req.getParameter("order_id");
        req.setAttribute("order_sum", detailService.find_sum(Long.valueOf(order_id)).get(0));
        req.getRequestDispatcher("master.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String order_id=req.getParameter("order_id");
        String action=req.getParameter("action");
        if(action.equals("back")){
//            上一步
            req.setAttribute("order_id",order_id);
            List<Info> info=infoService.findAll();
            req.setAttribute("info",info);
            req.getRequestDispatcher("detail.jsp").forward(req,resp);
        }else if(action.equals("del")){
//            退出
            detailService.del(order_id);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }else {
            int order_status= OrderStatus.UNFINISHED.getCode();
            int pay_status=OrderStatus.UNFINISHED.getCode();
//             购买和保存
            if(action.equals("")){
                order_status=OrderStatus.FINISHED.getCode();
                pay_status=OrderStatus.FINISHED.getCode();
            }
            Master master=new Master();
//            订单号
            master.setOrder_id(order_id);
//            买家信息
            master.setBuyer_name(req.getParameter("buyer_name"));
            master.setBuyer_phone(req.getParameter("buyer_phone"));
            master.setBuyer_address(req.getParameter("buyer_address"));
            master.setBuyer_openid(req.getParameter("buyer_openid"));
//            总金额
            master.setOrder_amount(Double.valueOf(req.getParameter("order_amount")));
//          总数量
            master.setOrder_total(req.getParameter("order_total"));
//            状态
            master.setOrder_status(order_status);
            master.setPay_status(pay_status);
//          保存与购买
            masterService.insert(master);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }
    }
}
